<script setup lang="ts">
  import { HMessageBox } from '@/components/common/message'
  import { useSourceForm, useGetSourceDetail } from '@/services/pages/order/useSoruce'
  import { ElForm, ElFormItem, ElInput, ElRadioGroup, ElRadio, FormInstance } from 'element-plus'
  import { ref, watch } from 'vue'

  const { sourceDetail, getDetailLoading, getDetail } = useGetSourceDetail()
  const { form, saveLoading, save, resetForm } = useSourceForm()

  defineOptions({
    name: 'OrderSourceCreateModal',
  })

  const props = defineProps({
    sourceId: {
      type: String,
      default: () => '',
    },
  })

  const emits = defineEmits<{
    (e: 'update'): void
  }>()

  // 弹窗显示状态
  const visible = defineModel('visible', { type: Boolean, default: false })

  // 表单组件实例
  const formInstance = ref<FormInstance | null>(null)

  // 表单验证规则
  const rules = {
    name: [{ required: true, message: '请输入来源名称', trigger: ['change', 'blur'] }],
    status: [{ required: true, message: '请选择状态', trigger: ['change', 'blur'] }],
    defaultSelect: [{ required: true, message: '请选择是否为默认值', trigger: ['change', 'blur'] }],
  }

  /**
   * 隐藏弹窗
   */
  const hideDialog = () => {
    visible.value = false
    resetForm()
  }

  /**
   * 确定事件
   */
  const onConfirm = async () => {
    if (formInstance.value) {
      try {
        await formInstance.value.validate()
        const res = await save()
        if (res.code === 20000) {
          hideDialog()
          emits('update')
          return
        }

        if (res.code === 400305) {
          HMessageBox.error('名称重复')
          return
        }
        HMessageBox.error(res.message)
      } catch (error) {
        // error
      }
    }
  }

  watch(visible, async (value) => {
    if (value && props.sourceId) {
      await getDetail(props.sourceId)
      form.value = sourceDetail.value
    }
  })
</script>

<template>
  <h-dialog
    v-model="visible"
    :title="props.sourceId ? '添加来源' : '修改来源'"
    :loading="saveLoading || getDetailLoading"
    @close="hideDialog"
  >
    <el-form
      ref="formInstance"
      label-position="top"
      class="modal_form_content"
      :model="form"
      :rules="rules"
      inline-message
    >
      <!-- 来源名称 -->
      <el-form-item label="来源名称" prop="name">
        <el-input v-model.trim="form.name" placeholder="请输入来源名称" maxlength="50" />
      </el-form-item>

      <div class="radio-container">
        <!-- 状态 -->
        <el-form-item label="状态" class="same_line" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio :value="false" size="small">启用</el-radio>
            <el-radio :value="true" size="small">禁用</el-radio>
          </el-radio-group>
        </el-form-item>

        <!-- 是否为默认值 -->
        <el-form-item label="是否为默认值" class="same_line" prop="defaultSelect">
          <el-radio-group v-model="form.defaultSelect">
            <el-radio-group v-model="form.defaultSelect">
              <el-radio :value="true" size="small">是</el-radio>
              <el-radio :value="false" size="small">否</el-radio>
            </el-radio-group>
          </el-radio-group>
        </el-form-item>
      </div>
    </el-form>

    <template #footer>
      <h-button type="primary" is-modal @click="onConfirm">确定</h-button>
      <h-button is-modal @click="hideDialog">取消</h-button>
    </template>
  </h-dialog>
</template>

<style scoped lang="scss">
  .radio-container {
    display: grid;
    grid-template-columns: repeat(2, 200px);
  }
</style>
